﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenuMasterForm.aspx.cs"
    Inherits="PSS.WebSite.SystemManage.MenuMasterForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>系统菜单</title>
    <script src="../lib/boot.js" type="text/javascript"></script>
</head>
<body>
    <div class="mini-splitter" style="width: 100%; height: 100%;">
        <div size="20%" showcollapsebutton="true">
            <div class="mini-fit">
                <ul id="tree1" class="mini-tree" style="width: 100%;" showtreeicon="true" textfield="MenuName"
                    idfield="MenuId" parentfield="ParentMenuId" resultastree="false" expandonload="true"
                    contextmenu="#treeMenu">
                </ul>
                <ul id="treeMenu" class="mini-contextmenu" onbeforeopen="onBeforeOpen">
                    <li name="add" iconcls="icon-add" onclick="onAddNode">新增节点</li>
                    <li name="edit" iconcls="icon-edit" onclick="onEditNode">编辑节点</li>
                    <li name="remove" iconcls="icon-remove" onclick="onRemoveNode">删除节点</li>
                </ul>
            </div>
        </div>
        <div size="80%" showcollapsebutton="true">
            <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
                <a class="mini-button" iconcls="icon-add" id="add" onclick="add();" plain="true">添加</a>
                <a class="mini-button" iconcls="icon-edit" id="edit" onclick="edit()" plain="true">编辑</a>
                <a class="mini-button" iconcls="icon-remove" id="remove" onclick="remove()" plain="true">
                    删除</a> <span class="separator"></span><a class="mini-button" onclick="close()" iconcls="icon-close"
                        plain="true">关闭</a>
            </div>
            <div class="mini-fit">
                <div id="grid1" class="mini-datagrid" style="width: 100%; height: 100%;" idfield="MenuId"
                    borderstyle="border:0;" sizelist="[10,20,50,100]" pagesize="20">
                    <div property="columns">
                        <div type="indexcolumn">
                        </div>
                        <div field="MenuName" width="30%" headeralign="center" allowsort="true">
                            菜单名称
                        </div>
                        <div field="Url" width="60%" headeralign="center" allowsort="true">
                            连接地址
                        </div>
                        <div field="MenuIdx" width="10%" headeralign="center" allowsort="true">
                            排序
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        mini.parse();
        var tree = mini.get("tree1");
        tree.setUrl("../Service/SystemManageHandler.ashx?action=GetMenuParentNode");
        var grid = mini.get("grid1");
        grid.setUrl("../Service/SystemManageHandler.ashx?action=GetMenuChildNode");
        tree.on("nodeselect", function (e) {
            if (e.isLeaf) {
                grid.load({ MenuId: e.node.MenuId });
            } else {
                grid.setData([]);
                grid.setTotalCount(0);
            }
        });

        function onBeforeOpen(e) {
            var menu = e.sender;
            var node = tree.getSelectedNode();
            var addItem = mini.getbyName("add", menu);
            var editItem = mini.getbyName("edit", menu);
            var removeItem = mini.getbyName("remove", menu);
            if (!node) {
                e.cancel = true;
                return;
            }
            if (node && node.MenuId == "00000000-0000-0000-0000-000000000000") {
                addItem.show();
                removeItem.hide();
                editItem.hide();
            }
            else {
                addItem.show();
                editItem.show();
                removeItem.show();
            }

        }

        function onAddNode(e) {
            var node = tree.getSelectedNode();
            if (node != null) {
                mini.open({
                    url: "SystemManage/ParentMenuNodeInfoForm.aspx",
                    title: "添加", width: 444, height: 202,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { action: "new", MenuId: node.MenuId };
                        iframe.contentWindow.SetData(data);
                    },
                    ondestroy: function (action) {
                        tree.load("../Service/SystemManageHandler.ashx?action=GetMenuParentNode");
                    }
                });
            }
        }

        function onEditNode(e) {
            var node = tree.getSelectedNode();
            if (node != null) {
                mini.open({
                    url: "SystemManage/ParentMenuNodeInfoForm.aspx",
                    title: "编辑" + node.MenuName, width: 444, height: 202,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { action: "edit", MenuId: node.MenuId };
                        iframe.contentWindow.SetData(data);

                    },
                    ondestroy: function (action) {
                        tree.load("../Service/SystemManageHandler.ashx?action=GetMenuParentNode");
                    }
                });
            }
        }

        function onRemoveNode(e) {
            var node = tree.getSelectedNode();
            if (node) {
                mini.confirm("您确定删除:" + node.MenuName + "？如果被删除旗下的子节点同时也被删除", "确定？",
                function (action) {
                    if (action == "ok") {
                        $.ajax({
                            url: "../service/SystemManageHandler.ashx?action=RemoveParentMenuNode&MenuId=" + node.MenuId,
                            success: function (text) {
                                tree.load("../Service/SystemManageHandler.ashx?action=GetMenuParentNode");
                                grid.reload();
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert(jqXHR.responseText);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            }
        }

        function add() {
            var node = tree.getSelectedNode();
            if (node != null) {
                if (node.MenuId == "00000000-0000-0000-0000-000000000000") {
                    mini.alert("请选择" + node.MenuName + "下的节点菜单再添加！");
                }
                else {
                    mini.open({
                        url: "SystemManage/MenuInfoForm.aspx",
                        title: "新增菜单", width: 400, height: 170,
                        onload: function () {
                            var iframe = this.getIFrameEl();
                            var data = { action: "new", ParentMenuId: node.MenuId };
                            iframe.contentWindow.SetData(data);
                        },
                        ondestroy: function (action) {
                            grid.reload();
                        }
                    });
                }
            }
            else {
                mini.alert("请选择主菜单下的菜单节点！");
            }
        }

        function edit() {
            var row = grid.getSelected();
            if (row) {
                mini.open({
                    url: "SystemManage/MenuInfoForm.aspx",
                    title: "编辑菜单", width: 400, height: 170,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { action: "edit", MenuId: row.MenuId };
                        iframe.contentWindow.SetData(data);

                    },
                    ondestroy: function (action) {
                        grid.reload();

                    }
                });

            } else {
                mini.alert("请选中一条记录!");
            }
        }

        function remove() {
            var rows = grid.getSelected();
            if (rows) {
                mini.confirm("您确定删除:" + rows.MenuName + "？", "确定？",
            function (action) {
                if (action == "ok") {
                    $.ajax({
                        url: "../service/SystemManageHandler.ashx?action=RemoveMenu&MenuId=" + rows.MenuId,
                        success: function (text) {
                            grid.reload();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {
                    return false;
                }
            });
            } else {
                mini.alert("请选中一条您需要删除的记录!");
            }
        }

        function CloseWindow(action) {
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }
        //关闭
        function close() {
            CloseWindow("close");
        }
    </script>
</body>
</html>
